package com.yeyks.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.hotel.VipOpenCityAddParam;
import com.yeyks.common.param.hotel.VipOpenCityUpdateParam;
import com.yeyks.common.utils.LocationUtils;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.common.utils.PinyinUtils;
import com.yeyks.common.utils.webutil.Arguments;
import com.yeyks.common.utils.webutil.CityUtils;
import com.yeyks.vip.dal.dao.VipOpenCityMapper;
import com.yeyks.vip.dal.domain.VipOpenCity;
import com.yeyks.vip.service.VipOpenCityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * vip开放城市 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-12-26
 */
@Service
public class VipOpenCityServiceImpl extends ServiceImpl<VipOpenCityMapper, VipOpenCity> implements VipOpenCityService {

    //开放城市
    @Override
    public List<VipOpenCity> enableList(Integer hot) {
        List<VipOpenCity> list = list(
                new QueryWrapper<VipOpenCity>()
                        .eq(VipOpenCity.STATUS, 1)
                        .eq(Objects.nonNull(hot), VipOpenCity.HOT, hot)
        );
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertCity(VipOpenCityAddParam param) {

        VipOpenCity vipOpenCity = ObjectConvertUtil.convertObject(param, VipOpenCity.class);
        //得到经纬度
        CityUtils cityUtils = LocationUtils.fromBaiDu(param.getName());
        if(null != cityUtils && cityUtils.getResult() != null && cityUtils.getResult().getLocation() != null){
            //存入经纬度
            vipOpenCity.setLnt(String.valueOf(cityUtils.getResult().getLocation().getLng()));
            vipOpenCity.setLat(String.valueOf(cityUtils.getResult().getLocation().getLat()));
        }
        //存入拼音信息
        vipOpenCity.setPinyin(PinyinUtils.getPinyinUpperCase(param.getName()));
        vipOpenCity.setGmtCreate(new Date());
        int insert = baseMapper.insert(vipOpenCity);
        return insert > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCity(VipOpenCityUpdateParam param) {

        //检查ID是否存在
        VipOpenCity city = baseMapper.selectById(param.getId());
        if(city == null){
            throw new ServiceException("主键ID不存在");
        }
        city = ObjectConvertUtil.convertObject(param, VipOpenCity.class);
        //得到经纬度
        CityUtils cityUtils = LocationUtils.fromBaiDu(param.getName());
        if(null != cityUtils && cityUtils.getResult() != null && cityUtils.getResult().getLocation() != null){
            //存入经纬度
            city.setLnt(String.valueOf(cityUtils.getResult().getLocation().getLng()));
            city.setLat(String.valueOf(cityUtils.getResult().getLocation().getLat()));
        }
        //存入拼音信息
        city.setPinyin(PinyinUtils.getPinyinUpperCase(param.getName()));
        int update = baseMapper.updateById(city);
        return update > 0;
    }

    @Override
    public Boolean syncAllPinyin() {
        //查询所有城市
        List<VipOpenCity> vipOpenCities = baseMapper.selectList(new QueryWrapper<>());
        if(!CollectionUtils.isEmpty(vipOpenCities)){
            vipOpenCities.forEach(vipOpenCity -> {
                if(Arguments.notEmpty(vipOpenCity.getName())){
                   vipOpenCity.setPinyin(PinyinUtils.getPinyinUpperCase(vipOpenCity.getName()));
                }
            });
            return super.updateBatchById(vipOpenCities);
        }
        return false;
    }

}
